<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="kafka唯一标志" label-width="110px" prop="kafkaId">
        <el-input
          v-model="queryParams.kafkaId"
          placeholder="请输入kafka唯一标志"
          style="width: 240px"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="连接地址" label-width="110px" prop="bootstrapServers">
        <el-input
          v-model="queryParams.bootstrapServers"
          placeholder="请输入连接地址"
          style="width: 240px"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="kafkaconfigList" @selection-change="handleSelectionChange" height="330px">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" width="55" align="center">
        <template slot-scope="scope">
          <span>{{ (pageParams.current-1)*pageParams.size + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="kafka唯一标志" align="center" prop="kafkaId" />
      <el-table-column label="连接地址" align="center" prop="bootstrapServers" />
      <el-table-column label="证书地址" align="center" prop="zsdz" />
      <el-table-column label="ACK方式" align="center" prop="producerAcks">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.kfk_ack" :value="scope.row.producerAcks"/>
        </template>
      </el-table-column>
      <el-table-column label="重试次数" align="center" prop="producerRetries" />
      <el-table-column label="批次消息数量" align="center" prop="producerBatchSize" />
      <el-table-column label="缓冲区大小" align="center" prop="producerBufferMemory" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:kafkaconfig:edit']"
          >修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="pageParams.current"
      :limit.sync="pageParams.size"
      @pagination="getList"
    />

    <!-- 添加或修改kafka配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="连接地址" prop="bootstrapServers">
          <el-input v-model="form.bootstrapServers" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="证书地址" prop="zsdz">
          <el-input v-model="form.zsdz" placeholder="请输入证书地址" />
        </el-form-item>
        <el-form-item label="ACK方式" prop="producerAcks">
          <el-select v-model="form.producerAcks" style="display: block;" placeholder="请选择ACK方式">
            <el-option
              v-for="dict in dict.type.kfk_ack"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="重试次数" prop="producerRetries">
          <el-input v-model="form.producerRetries" placeholder="请输入重试次数" />
        </el-form-item>
        <el-form-item label="批次消息数量" prop="producerBatchSize">
          <el-input v-model="form.producerBatchSize" placeholder="请输入批次发送kafka的消息数量" />
        </el-form-item>
        <el-form-item label="缓冲区大小（KB）" prop="producerBufferMemory">
          <el-input v-model="form.producerBufferMemory" placeholder="请输入缓冲区大小" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listKafkaconfig, getKafkaconfig, updateKafkaconfig } from "@/api/system/kafkaconfig";

export default {
  name: "Kafkaconfig",
  dicts: ['kfk_ack'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // kafka配置表格数据
      kafkaconfigList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 分页参数
      pageParams: {
        current: 1,
        size: 10,
      },
      // 查询参数
      queryParams: {
        kafkaId: null,
        bootstrapServers: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        kafkaId: [
          { required: true, message: "kafka唯一标志不能为空", trigger: "blur" }
        ],
        bootstrapServers: [
          { required: true, message: "连接地址不能为空", trigger: "blur" }
        ],
        producerAcks: [
          { required: true, message: "ACK方式不能为空", trigger: "change" }
        ],
        producerRetries: [
          { required: true, message: "重试次数不能为空", trigger: "blur" }
        ],
        producerBatchSize: [
          { required: true, message: "批次消息数量不能为空", trigger: "blur" }
        ],
        producerBufferMemory: [
          { required: true, message: "缓冲区大小不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询kafka配置列表 */
    getList() {
      this.loading = true;
      listKafkaconfig(this.pageParams,this.queryParams).then(response => {
        this.kafkaconfigList = response.page.records;
        this.total = response.page.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        kafkaId: null,
        bootstrapServers: null,
        zsdz: null,
        producerAcks: null,
        producerRetries: null,
        producerBatchSize: null,
        producerBufferMemory: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.pageParams.current = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      var kafkaIdUpdate = row.kafkaId;
      if(row.isTrusted){
        kafkaIdUpdate = this.ids[0];
      }
      this.queryParams["kafkaId"] = kafkaIdUpdate;
      getKafkaconfig(this.queryParams).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改kafka配置";
        this.queryParams["kafkaId"] = null;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          updateKafkaconfig(this.form).then(response => {
            if(response.code==='200'&&response.data>0) {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }else {
              this.$modal.msgError("修改失败");
              this.open = false;
            }
          });
        }
      });
    }
  }
};
</script>
